﻿<%@ WebHandler Language="C#" Class="dialog2" %>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Collections.Generic;
using jsm.SqlHelper;
using dbcaac;

public class dialog2 : IHttpHandler
{
    public void ProcessRequest (HttpContext context) {

        string sqlOld = string.Format(@"SELECT jyxmName, (SELECT jcyzyName from [jcyZY] WHERE jcyzyId = aTbl.jyxmProf) AS jyxmProf 
                                        FROM [jiyuanXM] AS aTbl WHERE jyxmId = {0} AND jyxmNewFlag <> 1", context.Request["id"]);
        string sqlNew = string.Format(@"SELECT xmrzName, (SELECT jcyzyName from [jcyZY] WHERE jcyzyId = aTbl.xmrzProf) AS xmrzProf, xmrzChangedType 
                                        FROM [jiyuanXMLog] AS aTbl WHERE xmrzLogId = '{0}'", context.Request["logId"]);

        SqlDataReader oldContent = SqlServerHelper.ExecuteReader(sqlOld);
        SqlDataReader newContent = SqlServerHelper.ExecuteReader(sqlNew);

        string templateTable = @"<table cellpadding='0' cellspacing='0'>
                                    <tr><th style=""white-space: nowrap"" width=""100"">基元项目名称</th>
                                        <td style=""word-break:break-all"" width=""500"">{0}</td></tr>
                                    <tr><th>项目专业</th><td style=""word-break:break-all"" width=""500"">{1}</td></tr>
                                </table>";

        string blankTable = string.Format(templateTable, "", "");

        string chgType = string.Empty;
        string newTable = string.Empty;
        string oldTable = string.Empty;
        
        if (oldContent.Read())
        {
            oldTable = string.Format(
                                    templateTable, 
                                    oldContent.GetSqlValue(oldContent.GetOrdinal("jyxmName")).ToString().Replace("\"", "\"\""),
                                    oldContent.GetSqlValue(oldContent.GetOrdinal("jyxmProf")).ToString().Replace("\"", "\"\"")
                                    );
        }

        if (newContent.Read())
        {
            chgType = newContent.GetSqlValue(newContent.GetOrdinal("xmrzChangedType")).ToString();
            newTable = string.Format(
                                    templateTable,
                                    newContent.GetSqlValue(newContent.GetOrdinal("xmrzName")).ToString().Replace("\"", "\"\""),
                                    newContent.GetSqlValue(newContent.GetOrdinal("xmrzProf")).ToString().Replace("\"", "\"\"")
                                   );
        }

        context.Response.Write(@"<div class=""diff"">");
        context.Response.Write(@"<h2>差异比较（绿色代表新增的内容，红色代表删除的内容）：</h2>");        
        HtmlDiff htmlDiff = null;
        if ("DELETE" == chgType)
        {
            htmlDiff = new HtmlDiff(oldTable, blankTable);
        }
        else if ("NEW" == chgType)
        {
            htmlDiff = new HtmlDiff(blankTable, newTable);
        }
        else
        {
            htmlDiff = new HtmlDiff(oldTable, newTable);
        }
        
        context.Response.Write(htmlDiff.Build());
        context.Response.Write(@"</div>");

        context.Response.End(); 
    } 
    
    public bool IsReusable {
        get {
            return false;
        }
    }
}